const storage = weex.requireModule('storage');
const Utils = {

	getThemeColor(callback) {
		storage.getItem('theme', event => {
			if (event.result == "success") {
				if (event.data == "theme1") {
					callback('#2e3238', event.data, "46,50,56");
				}
				if (event.data == "theme2") {
					callback('#1B82D2', event.data, "27,130,210");
				}
				if (event.data == "theme3") {
					callback('#DF6766', event.data, "223,103,102");
				}
				if (event.data == "theme4") {
					callback('#DE7243', event.data, "222,114,67");
				}
				if (event.data == "theme5") {
					callback('#329C92', event.data, "50,156,146");
				}
				if (event.data == "theme6") {
					callback('#66538B', event.data, "102,83,139");
				}
				if (event.data == "theme7") {
					callback('#A80000', event.data, "168,0,0");
				}

			} else {
				callback('#1B82D2', "theme2", "27,130,210");
			}
		});

	},
	getUrlSearch(url, name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = url.slice(url.indexOf('?') + 1).match(reg);
		if (r != null) {
			try {
				return decodeURIComponent(r[2]);
			} catch (_e) {
				return null;
			}
		}
		return null;
	},
	_typeof(obj) {
		return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
	},
	isPlainObject(obj) {
		return Utils._typeof(obj) === 'object';
	},
	isString(obj) {
		return typeof (obj) === 'string';
	},
	isNonEmptyArray(obj = []) {
		return obj && obj.length > 0 && Array.isArray(obj) && typeof obj !== 'undefined';
	},
	isObject(item) {
		return (item && typeof item === 'object' && !Array.isArray(item));
	},
	isEmptyObject(obj) {
		return Object.keys(obj).length === 0 && obj.constructor === Object;
	},
	env: {
		isTaobao() {
			const {
				appName
			} = weex.config.env;
			return /(tb|taobao|淘宝)/i.test(appName);
		},
		isTrip() {
			const {
				appName
			} = weex.config.env;
			return appName === 'LX';
		},
		isBoat() {
			const {
				appName
			} = weex.config.env;
			return appName === 'Boat' || appName === 'BoatPlayground';
		},
		isWeb() {
			const {
				platform
			} = weex.config.env;
			return typeof (window) === 'object' && platform.toLowerCase() === 'web';
		},
		isIOS() {
			const {
				platform
			} = weex.config.env;
			return platform.toLowerCase() === 'ios';
		},
		/**
		 * 是否为 iPhone X
		 * @returns {boolean}
		 */


		isIPhoneX() {
			const {
				deviceHeight
			} = weex.config.env;
			if (Utils.env.isWeb()) {
				return typeof window !== undefined && window.screen && window.screen.width && window.screen.height && (parseInt(
					window.screen.width, 10) === 375) && (parseInt(window.screen.height, 10) === 812);
			}
			return Utils.env.isIOS() && deviceHeight === 2436;
		},
		isAndroid() {
			const {
				platform
			} = weex.config.env;
			return platform.toLowerCase() === 'android';
		},
		isAlipay() {
			const {
				appName
			} = weex.config.env;
			return appName === 'AP';
		},
		isTmall() {
			const {
				appName
			} = weex.config.env;
			return /(tm|tmall|天猫)/i.test(appName);
		},
		isAliWeex() {
			return Utils.env.isTmall() || Utils.env.isTrip() || Utils.env.isTaobao();
		},
		supportsEB() {
			const weexVersion = weex.config.env.weexVersion || '0';
			const isHighWeex = Utils.compareVersion(weexVersion, '0.10.1.4') && (Utils.env.isIOS() || Utils.env.isAndroid());
			const expressionBinding = weex.requireModule('expressionBinding');
			return expressionBinding && expressionBinding.enableBinding && isHighWeex;
		},

		/**
		 * 判断Android容器是否支持是否支持expressionBinding(处理方式很不一致)
		 * @returns {boolean}
		 */
		supportsEBForAndroid() {
			return (Utils.env.isAndroid()) && Utils.env.supportsEB();
		},

		/**
		 * 判断IOS容器是否支持是否支持expressionBinding
		 * @returns {boolean}
		 */
		supportsEBForIos() {
			return (Utils.env.isIOS()) && Utils.env.supportsEB();
		},

		/**
		 * 获取weex屏幕真实的设置高度 
		 * @returns {Number}
		 */
		getPageHeight() {
			const {
				env
			} = weex.config;
			// const navHeight = Utils.env.isWeb() ? 0 : (Utils.env.isIPhoneX() ? 176 : 132);

			let screenHeight = env.deviceHeight / env.deviceWidth * 750
			return screenHeight;
		}
	},

	/**
	 * 版本号比较
	 * @memberOf Utils
	 * @param currVer {string}
	 * @param promoteVer {string}
	 * @returns {boolean}
	 * @example
	 *
	 * const { Utils } = require('@ali/wx-bridge');
	 * const { compareVersion } = Utils;
	 * console.log(compareVersion('0.1.100', '0.1.11')); // 'true'
	 */
	compareVersion(currVer = '0.0.0', promoteVer = '0.0.0') {
		if (currVer === promoteVer) return true;
		const currVerArr = currVer.split('.');
		const promoteVerArr = promoteVer.split('.');
		const len = Math.max(currVerArr.length, promoteVerArr.length);
		for (let i = 0; i < len; i++) {
			const proVal = ~~promoteVerArr[i];
			const curVal = ~~currVerArr[i];
			if (proVal < curVal) {
				return true;
			} else if (proVal > curVal) {
				return false;
			}
		}
		return false;
	},
	/**
	 * 分割数组
	 * @param arr 被分割数组
	 * @param size 分割数组的长度
	 * @returns {Array}
	 */
	arrayChunk(arr = [], size = 4) {
		let groups = [];
		if (arr && arr.length > 0) {
			groups = arr.map((e, i) => {
				return i % size === 0 ? arr.slice(i, i + size) : null;
			}).filter(e => {
				return e;
			});
		}
		return groups;
	},
	truncateString(str, len, hasDot = true) {
		let newLength = 0;
		let newStr = '';
		let singleChar = '';
		const chineseRegex = /[^\x00-\xff]/g;
		const strLength = str.replace(chineseRegex, '**').length;
		for (let i = 0; i < strLength; i++) {
			singleChar = str.charAt(i).toString();
			if (singleChar.match(chineseRegex) !== null) {
				newLength += 2;
			} else {
				newLength++;
			}
			if (newLength > len) {
				break;
			}
			newStr += singleChar;
		}

		if (hasDot && strLength > len) {
			newStr += '...';
		}
		return newStr;
	},
	formatDate(date, fmt) {
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
		}
		let o = {
			'M+': date.getMonth() + 1,
			'd+': date.getDate(),
			'h+': date.getHours(),
			'm+': date.getMinutes(),
			's+': date.getSeconds()
		};
		for (let k in o) {
			if (new RegExp(`(${k})`).test(fmt)) {
				let str = o[k] + '';
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
			}
		}
		return fmt;
	},

	padLeftZero(str) {
		return ('00' + str).substr(str.length);
	}
};

export default Utils;
